import { create } from "zustand";
import { cityConstants } from "../class/City";
import { devtools } from 'zustand/middleware';

import Person from '../class/Person';

type State = {
  person: Person;
  name: string;
  cityName: keyof typeof cityConstants;
};

type Actions = {
  setPeron: (name: string) => void;
  setCityName: (cityName: keyof typeof cityConstants) => void;
  reset: () => void;
};

const person = new Person('张三');

const useStore = create<State & Actions>()(devtools((set) => ({
  person,
  name: person.getName(),
  cityName: person.city.cityName,
  setPeron: (name) =>
    set({ name: person.setName(name) }),
  setCityName: (cityName: keyof typeof cityConstants) =>
    set({ cityName: person.city.setCityName(cityName) }),
  reset: () => set(state => {
    const resetData = person.reset();

    return { ...state, ...resetData };
  })
})));

export default useStore;